<template>
  <div>
    <el-row class="mt5 mb5">
      <el-button type="primary" size="mini" @click="addTab">添加</el-button>
    </el-row>
    
    <el-tabs v-if="isInit" v-model="curTabId" size="mini" closable @tab-remove="removeTab" class="main-tab-pane">
        <el-tab-pane
                v-for="(item) in mainTabS"
                :key="item.name"
                :label="item.title"
                :name="item.name"
                class="main-tab-pane"
                >
            <cmdCfg :isBatch="true" :cfg="cfg"></cmdCfg>
        </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
  import cmdCfg from "./cmdCfg.vue"
  export default {
    components:{
      cmdCfg
    },
    data() {
      return {
        isInit : false,
        curTabId: '1',
        mainTabS: [],
        tabIndex: 0,
        cfg: {},
      };
    },
    mounted(){
      this.readConfig()
      this.addTab();
    },
    methods: {
      addTab: function () {
          var newTabName = ++this.tabIndex + '';
          this.mainTabS.push({
              title: "phone"+newTabName,
              name: newTabName
          });
          this.curTabId = newTabName;
      },
      removeTab: function (targetName) {
          var tabs = this.mainTabS;
          var activeName = this.curTabId;
          if (activeName === targetName) {
              tabs.forEach(function(tab, index) {
                  if (tab.name === targetName) {
                      var nextTab = tabs[index + 1] || tabs[index - 1];
                      if (nextTab) {
                          activeName = nextTab.name;
                      }
                  }
              });
          }
          this.curTabId = activeName;
          this.mainTabS = tabs.filter(function(tab){ return tab.name !== targetName});
      },
      readConfig(){
          var that = this;
          if(!window.electron){
              return;
          }
          this.registerListen(this,"setConfig",(cfg)=>{
              try{
                  cfg.adbCmdCfg = cfg.adbCmdCfg || {};
                  cfg.adbCmdCfg.cmdList = cfg.adbCmdCfg.cmdList || that.defCmd;
                  if(cfg.adbCmdCfg.cmdList.length>0){
                      if(!cfg.adbCmdCfg.cmdList[0].value){
                          var item = {name:"默认分组",value:"defCmd",cmds:cfg.adbCmdCfg.cmdList};
                          cfg.adbCmdCfg.cmdList = [item]
                      }
                  }
                  that.cfg = cfg;
                  that.isInit = true;
              }catch(ex){
                  that.$message.error("配置加载异常:"+ex.message)
              }
          });
          electron.ipcRenderer.send("getConfig","")

      },
    }
  };
</script>